// ShellExt.h : Declaration of the CShellExt

#pragma once
#include "resource.h"       // main symbols


#include "DesktopContextMenuExtension_i.h"
#include <shobjidl.h>
#include <functional>

#include "MenuInfo.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;


// CShellExt

class ATL_NO_VTABLE CShellExt :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CShellExt, &CLSID_ShellExt>,
	public IDispatchImpl<IShellExt, &IID_IShellExt, &LIBID_DesktopContextMenuExtensionLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IContextMenu,	/*For Inserting ContextMenuItem.*/
	public IShellExtInit	
{
public:
	CShellExt();

DECLARE_REGISTRY_RESOURCEID(IDR_SHELLEXT)


BEGIN_COM_MAP(CShellExt)
	COM_INTERFACE_ENTRY(IShellExt)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(IContextMenu)
	COM_INTERFACE_ENTRY(IShellExtInit)
END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

	virtual HRESULT STDMETHODCALLTYPE QueryContextMenu(__in HMENU hmenu, __in UINT indexMenu, __in UINT idCmdFirst, __in UINT idCmdLast, __in UINT uFlags) override;

	virtual HRESULT STDMETHODCALLTYPE InvokeCommand(__in CMINVOKECOMMANDINFO *pici) override;

	virtual HRESULT STDMETHODCALLTYPE GetCommandString(__in UINT_PTR idCmd, __in UINT uType, __reserved UINT *pReserved, __out_awcount(!(uType & GCS_UNICODE) , cchMax) LPSTR pszName, __in UINT cchMax) override;

	virtual HRESULT STDMETHODCALLTYPE Initialize(__in_opt PCIDLIST_ABSOLUTE pidlFolder, __in_opt IDataObject *pdtobj, __in_opt HKEY hkeyProgID) override;


	std::function<void(void*)> OnDesktopCommand;


	HMENU GenPopupMenu(const CMenuInfo& info,UINT cmdId);

	MENUITEMINFO GenMenuItemInfo(const CMenuInfo &info,UINT cmdId);

};

OBJECT_ENTRY_AUTO(__uuidof(ShellExt), CShellExt)
